package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Breakrulerecord;
import com.woniuxy.caradmin.entity.pojo.Insurerecord;
import com.woniuxy.caradmin.entity.vo.BreakrulerecordVo;
import com.woniuxy.caradmin.entity.vo.InsurerecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.InsurerecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/insurerecord")
public class InsurerecordController {
    @Autowired
    private InsurerecordService insurerecordService;

    @GetMapping("/insurerecordById")
    @ApiOperation(value = "id查询年检记录")
    public R insurerecordById(Integer id) {

        Insurerecord insurerecord = insurerecordService.selectInsureById(id);
        if (insurerecord !=null) {

            return R.ok().data("insurerecord",insurerecord);
        } else {
            throw new CarException(ResultCode.INSUREISNULL, Constaint.INSUREISNULL);
        }
    }




    @GetMapping("/insurerecordList")
    @ApiOperation("保险记录")
    public DataGridView insurerecordList(InsurerecordVo insurerecordVo){
        Page<Object> page = PageHelper.startPage(insurerecordVo.getPage(), insurerecordVo.getLimit());
        List<Insurerecord> insurerecords = insurerecordService.selectAllByPage(insurerecordVo);
        if(insurerecords!=null && insurerecords.size()>0){
            return  new DataGridView(page.getTotal(),insurerecords);
        }else {
            throw new CarException(ResultCode.INSUREISNULL , Constaint.INSUREISNULL);
        }
    }
    @GetMapping("/addinsurerecord")
    @ApiOperation("提交保险记录")
    public R addinsurerecord(Insurerecord insurerecord, HttpServletRequest httpServletRequest){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String vid = httpServletRequest.getParameter("vid");
        String insurenum = httpServletRequest.getParameter("insurenum");
        String starttime = httpServletRequest.getParameter("starttime");
        String typeid = httpServletRequest.getParameter("typeid");
        String money = httpServletRequest.getParameter("money");
        String companyid = httpServletRequest.getParameter("companyid");
        String endtime = httpServletRequest.getParameter("endtime");
        String driverid = httpServletRequest.getParameter("driverid");
        java.sql.Date et = null;
        java.sql.Date st = null;
        try {
           Date st1 = sdf.parse(starttime);
            st = new java.sql.Date(st1.getTime());
            Date et1 = sdf.parse(endtime);
            et = new java.sql.Date(et1.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        insurerecord.setStarttime(st);
        insurerecord.setEndtime(et);
        insurerecord.setTypeid(Integer.parseInt(typeid));
        insurerecord.setMoney(Double.parseDouble(money));
        insurerecord.setVid(Integer.parseInt(vid));
        insurerecord.setCompanyid(Integer.parseInt(companyid));
        insurerecord.setDriverid(Integer.parseInt(driverid));
        insurerecord.setInsurenum(insurenum);
         insurerecord.setStatus(1);
        int i= insurerecordService.addInsurerecord(insurerecord);
        if(i!=0){
            return R.ok().code(ResultCode.ADDINUSRESUC).message(Constaint.ADDINUSRESUC);
        }else {
            throw new CarException(ResultCode.ADDINSUREFAIL , Constaint.ADDINSUREFAIL);
        }
    }
    @GetMapping("/updateinsurerecord")
    @ApiOperation("更新保险记录")
    public  R updateinsurerecord(Insurerecord insurerecord){
        int i = insurerecordService.upInsurerecord(insurerecord);
        if(i!=0){
            return  R.ok().code(ResultCode.UPINSURESUC).message(Constaint.UPINSURESUC);
        }else {
            throw  new CarException(ResultCode.UPINSUREFAIL,Constaint.UPINSUREFAIL);
        }
    }
    @GetMapping("/removeinsurerecord")
    @ApiOperation("删除保险记录")
    public  R removeinsurerecord(Integer[] ids){
        boolean b = insurerecordService.removeInsurerecord(ids);
        if(b){
            return  R.ok().code(ResultCode.REMOVEINSURESUC).message(Constaint.REMOVEINSURESUC);
        }else {
            throw  new CarException(ResultCode.REMOVEINSUREFAIL,Constaint.REMOVEINSUREFAIL);
        }
    }
}

